package com.example.fuluchou.common.logininterceptor;

import com.example.fuluchou.common.untils.JWTUtil;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

@Slf4j
@Component
public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler){
        //获取请求头中的 token
        String token = request.getHeader("user_token");
        log.info("获取 token : {}",token);
        log.info("获取路径: {}",request.getRequestURL());

        //解析JWT令牌
        Claims claims = JWTUtil.parseJWT(token);
        if(null == claims){
            log.error("解析JWT令牌 失败！");
            response.setStatus(401);
            return false;
        }

        log.info("JWT 令牌解析成功，允许访问");
        return true;
    }



}
